using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;

/// <summary>
/// Summary description for Customer
/// </summary>
public class CustomerDAO:DBConnection
{
    #region sql command
    string SQL_SELECT_ID = "select @@IDENTITY from Customer";
    string SQL_SELECT_BY_ID = "select PersonID, Name, DateOfBirth, Address, Country, City,  CreditCard from Person join Customer on Person.PersonID= Customer.CustomerID where Customer.PersonID=?";
    string SQL_SELECT_ALL = "";
    string SQL_INSERT = "insert into Customer (PersonID, CreditCard) values(@personID,@creditCard)";
    string SQL_UPDATE = "update Customer set CreditCard = @creditCard where PersonID = @personId";
    string SQL_DELETE = "delete from Customer where PersonId = @personId";
    #endregion

    #region fields
    string message = "";   
    #endregion
    
    #region constructors
    public CustomerDAO()
    {
    }
    #endregion

    #region properties
    public string Message
    {
        get { return message; }       
    }
    #endregion

    #region methods
    public int InsertCustomer(Customer cus)
    {
        int effectedRow = 0;
        try
        {
            //loi o day, do id cua Custu tang
            SqlCommand cmd = new SqlCommand(SQL_INSERT, conn);
            cmd.Parameters.AddWithValue("@personID",cus.ID);
            cmd.Parameters.AddWithValue("@creditCard", cus.CreditCard);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();              
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }

    public int UpdateCustomer(Customer cus)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_UPDATE, conn);           
            cmd.Parameters.AddWithValue("@zipCode", cus.ZIPCode);
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;
    }

    public int DeleteCustomer(Customer cus)
    {
        int effectedRow = 0;
        try
        {
            SqlCommand cmd = new SqlCommand(SQL_DELETE, conn);
            cmd.Parameters.AddWithValue("@personId", cus.ID);           
            if (Connect())
            {
                effectedRow = cmd.ExecuteNonQuery();
                Disconnect();
            }
        }
        catch (SqlException ex) { message = ex.Message; }
        catch (Exception ex) { message = ex.Message; }
        return effectedRow;

    }

    #endregion
}
